//
//  DrugSearchViewController.m
//  TestARC
//
//  Created by 晓泉 吴 on 12-9-22.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DrugSearchViewController.h"
#import "DrugDetailViewController.h"

#define CommonViewTAG 100
#define SeggestionViewTAG 101

@interface DrugSearchViewController ()

@end

@implementation DrugSearchViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.navigationItem.title=@"药品";
    self.searchBar.placeholder = @"搜索药品";
    //设置提示文字
    self.hipLabel.text = [NSString stringWithFormat:@"常用药品"];
    //[self.dbHelper quary:nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)loadCommonData
{
    [self.commonArray removeAllObjects];
    FMResultSet *rs =[self.dbHelper quaryCommonDrug];
    while ([rs next]) {
        [self.commonArray addObject:[rs resultDictionary]];
    }
}

-(void)loadSeggestionData:(NSString *)queryText
{
    [self.seggestionArray removeAllObjects];
    FMResultSet *rs =[self.dbHelper searchDrugSeggestion:queryText];
    //NSLog(@"rs.query=%@",rs.query);
    while ([rs next]) {
        [self.seggestionArray addObject:[rs resultDictionary]];
    }
    
}

-(void)setCellImage:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //cell.imageView.image = [UIImage imageNamed:@"table_drug_logo.png"];
    UIImage *image = [UIImage imageNamed:@"table_drug_logo_23.png"];
    UIImage *stretchImage = [image stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
    cell.imageView.image = stretchImage;
}

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"将要选择第 %d 行",[indexPath row]);
    return indexPath;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //填装详细视图的内容。
    
    DrugDetailViewController * detailController = [[DrugDetailViewController alloc]initWithNibName:@"DrugDetailViewController" bundle:nil];
    
    int row = [indexPath row];
    //NSLog(@"选择了 %d 行",row);
    NSDictionary *drug;
    if (tableView.tag == CommonViewTAG) {
        drug = [self.commonArray objectAtIndex:row];
        
    } else {
        drug = [self.seggestionArray objectAtIndex:row];
    }
    detailController.name =       [drug objectForKey:@"name"];
    detailController.brandName =  [drug objectForKey:@"BrandName"];
    detailController.commonName = [drug objectForKey:@"CommonName"];
    detailController.queryId =    [drug objectForKey:@"queryid"];
    
    [self.navigationController pushViewController:detailController animated:YES];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
